import { Inject, Provide } from "@midwayjs/core";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { Repository } from "typeorm";
import { Utils } from "../utils";
import { ClubCreateDto, ClubPageQueryDto } from "../dto/club.dto";
import { BblClub } from "../entity/club.entity";
import { BblAttach } from "../entity/attach.entity";
import { AttachService } from "./att.service";
import { ClubDto } from "../dto/club.dto";

@Provide()
export class ClubService {
  @Inject()
  utils: Utils;

  @Inject()
  attService: AttachService;

  /**
   * 附件驱动模型
   */
  @InjectEntityModel(BblClub)
  clubModel: Repository<BblClub>;

  async add_club(dto: ClubCreateDto) {
    const club = new BblClub();
    club.clubId = this.utils.uuid();
    club.clubMainName = dto.clubMainName;
    club.clubSubName = dto.clubSubName;
    club.clubPrice = dto.clubPrice;
    club.clubImg = new BblAttach();
    club.clubImg.attId = dto.attId;
    const result = await this.clubModel.save(club);
    const fileRes = await this.attService.get_att_by_id(dto.attId);
    await this.attService.tmp_to_uploads(fileRes.data);

    return this.utils.service.success(result);
  }

  async update_club(clubId: string, dto: ClubCreateDto) {
    const club = await this.clubModel
      .createQueryBuilder("club")
      .leftJoinAndSelect("club.clubImg", "clubImg")
      .where("club.clubId = :clubId", { clubId })
      .getOne();
    if (!club) {
      return this.utils.service.fail("球杆不存在");
    } else {
      club.clubMainName = dto.clubMainName;
      club.clubSubName = dto.clubSubName;
      club.clubPrice = dto.clubPrice;
      if (club.clubImg.attId !== dto.attId) {
        this.attService.remove(club.clubImg);
        const fileRes = await this.attService.get_att_by_id(dto.attId);
        club.clubImg = fileRes.data;
        await this.attService.tmp_to_uploads(fileRes.data);
      }
      const result = await this.clubModel.save(club);
      return this.utils.service.success(result);
    }
  }

  async delete_club(clubId: string) {
    const club = await this.clubModel
      .createQueryBuilder("club")
      .leftJoinAndSelect("club.clubImg", "clubImg")
      .where("club.clubId = :clubId", { clubId })
      .getOne();
    if (!club) {
      return this.utils.service.fail("球杆不存在");
    } else {
      this.attService.remove(club.clubImg);
      await this.clubModel.remove(club);
      return this.utils.service.success(clubId);
    }
  }

  async get_club_list() {
    const builder = this.clubModel
      .createQueryBuilder("club")
      .leftJoinAndSelect("club.clubImg", "clubImg")
      .orderBy("club.createAt", "DESC");

    const clubs = await builder.getMany();
    const list = clubs.map((club) => new ClubDto(club));

    return this.utils.service.success(list);
  }

  async get_club_page_list(dto: ClubPageQueryDto) {
    const builder = this.clubModel
      .createQueryBuilder("club")
      .leftJoinAndSelect("club.clubImg", "clubImg")
      .where("1=1")
      .orderBy("club.createAt", "DESC");

    if (dto.clubMainName) {
      builder.andWhere("club.clubMainName like :clubMainName", {
        clubMainName: `%${dto.clubMainName}%`
      });
    }

    if (dto.clubSubName) {
      builder.andWhere("club.clubSubName like :clubSubName", {
        clubSubName: `%${dto.clubSubName}%`
      });
    }
    const total = await builder.getCount();

    builder.skip((dto.current - 1) * dto.pageSize);
    builder.take(dto.pageSize);

    const list = await builder.getMany();
    const records = list.map((d) => new ClubDto(d));

    return this.utils.service.success({
      total,
      records,
      current: Number(dto.current),
      pageSize: Number(dto.pageSize)
    });
  }
}
